package com.justgame.project.generator.verify.impl;

import com.justgame.project.generator.schema.Entity;
import com.justgame.project.generator.schema.Enumerate;
import com.justgame.project.generator.schema.Field;
import com.justgame.project.generator.verify.IEntityValidation;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;
import java.util.stream.Collectors;

@Component
public class EnumEntityValidation implements IEntityValidation {
    @Override
    public void valid(Entity entity) {
        int enumFieldSize = (int) entity.getFields().stream().filter(Field::isEnum).count();
        List<Enumerate> enums = entity.getEnums();
        Assert.isTrue(enumFieldSize == enums.size(),"存在未定义枚举类型或枚举为空的字段。");
        List<String> fieldNames = entity.getFields().stream().map(Field::getName).collect(Collectors.toList());
        enums.forEach(enu->{
            Assert.isTrue(fieldNames.contains(enu.getFieldName()),
                    String.format("实体类 %s 构建中不存在 %s 字段",entity.getName(),enu.getFieldName()));
            Assert.isTrue(enu.getEnumName().matches("[a-zA-Z0-9]+"),
                    String.format(" %s 的枚举值类型名不能包含特殊字符",enu.getFieldName()));
        });
    }

    @Override
    public int sort() {
        return 0;
    }
}
